package com.letfox.auth.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.letfox.common.model.ResultResponse;
import com.letfox.common.util.http.HTTPResponseUtil;


@Controller
public class BaseController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	
	protected ResultResponse resultResponse;

	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
	}

	@ModelAttribute
	public void setRequestAndResponse(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.session = request.getSession();
		 this.resultResponse = new ResultResponse();
	}

	/**
	 * 获取项目路径
	 *
	 * @return
	 */
	protected String getRootPath() {
		return getRequest().getSession().getServletContext().getRealPath("/");
	}
	
	 /**
     * 输出对象客户端
     * 
     * @param data
     *            输出的数据对象
     * @throws IOException
     */
    protected void print(Object data, HttpServletResponse response) {
        print(data.toString(), response);
    }

    /**
     * 输出String客户端
     * 
     * @param data
     *            输出的数据对象
     * @throws IOException
     */
    private void print(String data, HttpServletResponse response) {
        if (null == data) {
            logger.warn("data is null");
            data = "";
        }
        HTTPResponseUtil.renderText(response, data);
    }
}
